/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package suministros.ejb30.DAO;

import java.util.StringTokenizer;
import javax.activation.DataHandler;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.mail.BodyPart;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.naming.NamingException;

/**
 *
 * @author Renato
 */
@Stateless
public class SumiEMail implements SumiEMailLocal {
    @Resource(name = "mail/SesionLogistica")
    private Session mailSesionLogistica;

    private String[] obtenerDireccionesTokenizer(String dir){
        String [] retorno;
        StringTokenizer recepientsTokenizer = new StringTokenizer(dir.trim(), ",");
        retorno = new String[recepientsTokenizer.countTokens()];
        for (int i = 0; i < retorno.length; i++) {
                    retorno[i] = recepientsTokenizer.nextToken();
                }
        return retorno;
    }
    public void enviarEmail(String email, String subject, String body) throws NamingException, MessagingException {
        MimeMessage message = new MimeMessage(mailSesionLogistica);
        message.setSubject(subject);
        String[] direccionesTO=obtenerDireccionesTokenizer(email);
        for(int i=0;i<direccionesTO.length;i++){
            message.addRecipients(RecipientType.TO, InternetAddress.parse(direccionesTO[i], false));
        }
        
        message.setContent(body, "text/html; charset=\"ISO-8859-2\"");
        /*Transport tr = mailSesionLogistica.getTransport("smtp");
        tr.connect("admin@epsgrau.com.pe", "123456");
        tr.send(message);
        tr.close();*/
    }

    public void enviarEmailComplejo(String email, String subject, String body,String file_name, DataHandler archivo) throws NamingException, MessagingException {
        
        //message.setText(body);

        // Se compone la parte del texto
         BodyPart texto = new MimeBodyPart();
         texto.setText(body);

          BodyPart adjunto = new MimeBodyPart();
         adjunto.setDataHandler(archivo);
         adjunto.setFileName(file_name);

         // Una MultiParte para agrupar texto e imagen.
         MimeMultipart multiParte = new MimeMultipart();
         multiParte.addBodyPart(texto);
         multiParte.addBodyPart(adjunto);

         MimeMessage message = new MimeMessage(mailSesionLogistica);
        message.setSubject(subject);

        String[] direccionesTO=obtenerDireccionesTokenizer(email);
        for(int i=0;i<direccionesTO.length;i++){
            message.addRecipients(RecipientType.TO, InternetAddress.parse(direccionesTO[i], false));
        }
        message.setContent(multiParte);

        Transport tr = mailSesionLogistica.getTransport("smtp");
        tr.connect("admin@epsgrau.com.pe", "123456");
        tr.send(message);
        tr.close();
    }

    
   
 
}
